<?php
class NodeController extends CommonController{
	public function Index(){
		$menu_item	= $this->cache->read('menu_item');
		$list = $this->db->table("@_role_node")->order("sort ASC, menu_id ASC")->findAll();
		foreach ($list as $key => $v) {
			$list[$key]['menu_name']	= $menu_item[$v['menu_id']];
			if ($v['is_use'] == 1) {
				$list[$key]['use_name']		= '是';
			} else {
				$list[$key]['use_name']		= '否';
			}
		}
		include $this->display('node.html');
	}

	public function Add(){
		$isPost		= isset($_GET['isPost']) ? intval($_GET['isPost']) : 0;
		if (subMit('data')) {
			$data				= getGpc('data', 'string', 'P');
			$data['sort']		= intval($data['sort']);
			$data['is_use']		= intval($data['is_use']);
			$data['menu_id']	= intval($data['menu_id']);

			if (empty($data['name'])) {
				$this->msg('请填写名称。');
			}
			if (empty($data['controller'])) {
				$this->msg('请填写控制器');
			}
			if (empty($data['operate'])) {
				$this->msg('请填写操作');
			}
			if (empty($data['menu_id'])) {
				$this->msg('请选择导航');
			}
			$this->db->table("@_role_node")->add($data);
			$this->_cache();
			$result['status']	= 1;
			$result['msg']		= '发布成功';
			echo json_encode($result);
			exit();

		}
		$list = $this->db->table("@_menu")->order("sort ASC, id ASC")->findAll();
		ob_start();
		include $this->display('node.html');
		$content = ob_get_contents() ;
		ob_end_clean() ;
		$result['status']	= 1;
		$result['content']	= $content;
		echo json_encode($result);
	}

	public function Edit(){
		$id 		= getGpc('id', 'integer', 'G');
		if (empty($id)) {
			$this->msg('参数错误。');
		}
		if ( isset($_POST) && $_POST ){
			$data				= getGpc('data', 'string', 'P');
			$data['sort']		= intval($data['sort']);
			$data['is_use']		= intval($data['is_use']);
			$data['menu_id']	= intval($data['menu_id']);

			if (empty($data['name'])) {
				$this->msg('请填写名称。');
			}
			if (empty($data['controller'])) {
				$this->msg('请填写控制器');
			}
			if (empty($data['operate'])) {
				$this->msg('请填写操作');
			}
			if (empty($data['menu_id'])) {
				$this->msg('请选择导航');
			}
			$this->db->table("@_role_node")->where("id=$id")->update($data);
			$this->_cache();
			$result['status']	= 1;
			$result['msg']		= '修改成功';
			echo json_encode($result);
			exit();
		}
		$vo = $this->db->table("@_role_node")->where("id=$id")->find();
		if (empty($vo)) {
			$this->msg('不存在此菜单');
		}
		$list = $this->db->table("@_menu")->order("sort ASC, id ASC")->findAll();
		ob_start();
		include $this->display('node.html');
		$content = ob_get_contents() ;
		ob_end_clean() ;
		$result['status']	= 1;
		$result['content']	= $content;
		echo json_encode($result);
	}

	public function Del(){
		$id 		= getGpc('id', 'integer', 'G');
		if (empty($id)) {
			$this->msg('参数错误。');
		}
		$this->db->table("@_role_node")->where("id=$id")->delete();
		$this->_cache();
		$result['status']	= 1;
		echo json_encode($result);
	}

	// 更新缓存
	private function _cache(){
		$list     = $this->db->table("@_menu")->order("sort ASC")->findAll();
		$menu_num = count($list);
		foreach ($list as $key => $v) {
			$list[$key]['num']	= $menu_num;
			$list[$key]['node']	= $this->db->table("@_role_node")->where("menu_id={$v['id']}")->order("sort ASC, id ASC")->findAll();
		}
		$this->cache->write($list, 'menu_list');
	}
}
?>